﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using System.Text;
using System.Globalization;
using System.IO;

public partial class Report_StorageTime : System.Web.UI.Page
{
    DbHelper access = new DbHelper();
    DbCommand cmd = null;
    static DataSet ds = null;
    string strSQL = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        bool isPostBack = base.IsPostBack;
    }

    // Methods
    protected void bind()
    {
        strSQL = @"SELECT '' +  CAST(TBP.Pos_ID AS VARCHAR) AS Pos_ID,TBS.Sec_Mat_ID,'' +  CAST(TBT.Tray_ID AS VARCHAR) AS Tray_ID,'' +  CAST(TBM.Mat_Code AS VARCHAR) AS Mat_Code,TBM.Mat_Name,TBS.CDT FROM TBase_Storage TBS	
	                        LEFT JOIN TBase_Interface TBI ON TBI.Sec_Mat_ID=TBS.Sec_Mat_ID
	                        LEFT JOIN TBase_Material TBM ON TBM.Mat_Code=TBI.Mat_Code
	                        LEFT JOIN TRel_TrayPosContainer TRT ON TRT.ContainerID=TBS.ContainerID
	                        LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID=TRT.Relations_ID
	                        LEFT JOIN TRelations TRP ON TRP.Tray_ID=TBT.Tray_ID
	                        LEFT JOIN TBase_Position_Shuttle TBP ON TBP.Pos_ID=TRP.Pos_ID
	                        WHERE TBP.Pos_ID<>'200000000000'";
        if (txtMat_Code.Text.Trim() != "")
        {
            strSQL += " AND TBM.Mat_Code LIKE '%" + txtMat_Code.Text.Trim() + "%'";
        }
        if (txtMat_Name.Text.Trim() != "")
        {
            strSQL += " AND TBM.Mat_Name LIKE '%" + txtMat_Name.Text.Trim() + "%'";
        }
        if (this.getDataTime1.BegTime.ToString().Trim() != "")
        {
            strSQL += " AND TBS.CDT >= convert(datetime,'" + this.getDataTime1.BegTime.ToString() + "')";
        }
        if (this.getDataTime1.EndTime.ToString().Trim() != "")
        {
            strSQL += " AND TBS.CDT <=dateadd(d,1,convert(datetime,'" + this.getDataTime1.EndTime.ToString() + "'))";
        }
        strSQL += " ORDER BY Pos_ID";
        this.cmd = this.access.GetSqlStringCommond(this.strSQL);
         ds = this.access.ExecuteDataSet(this.cmd);
        this.gv.DataSource =  ds.Tables[0].DefaultView;
        this.gv.DataBind();
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gv.PageIndex = e.NewPageIndex;
        this.bind();
    }

    protected void ibtnExcel_Click(object sender, ImageClickEventArgs e)
    {
        //base.Response.Clear();
        //base.Response.BufferOutput = true;
        //base.Response.Charset = "utf-8";
        //DateTime now = DateTime.Now;
        //base.Response.AppendHeader("Content-Disposition", "attachment;filename=" + now.ToString("yyyyMMddHHmmss") + ".xls");
        //base.Response.ContentEncoding = Encoding.GetEncoding("utf-8");
        //base.Response.ContentType = "application/ms-excel";
        //this.EnableViewState = false;
        //CultureInfo formatProvider = new CultureInfo("ZH-CN", true);
        //StringWriter writer = new StringWriter(formatProvider);
        //HtmlTextWriter writer2 = new HtmlTextWriter(writer);
        //this.gv.RenderControl(writer2);
        //base.Response.Write(writer.ToString());
        //base.Response.End();
        DataTable dt = ds.Tables[0];
        Response.Clear();
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Charset = "GB2312";
        Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode("库龄报表" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"));
        Response.ContentType = "application/ms-excel";

        Response.Write("货位号\t托盘号\t物料编码\t物料名称\t入库日期");

        Response.Write(Environment.NewLine);
        foreach (DataRow dr in dt.Rows)
        {

            StringBuilder sb = new StringBuilder();

            sb.Append(dr["Pos_ID"] + "\t" + dr["Tray_ID"] + "\t" + dr["Mat_Code"] + "\t" + dr["Mat_Name"] + "\t" + Convert.ToDateTime(dr["CDT"]).ToString("yyyy-MM-dd"));

            Response.Write(sb.ToString());
            Response.Write(Environment.NewLine);

        }
        Response.End();
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        bind();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
   
}
